/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "system.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for Control */
osThreadId_t ControlHandle;
const osThreadAttr_t Control_attributes = {
  .name = "Control",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Propeller */
osThreadId_t PropellerHandle;
const osThreadAttr_t Propeller_attributes = {
  .name = "Propeller",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Usart1Task */
osThreadId_t Usart1TaskHandle;
const osThreadAttr_t Usart1Task_attributes = {
  .name = "Usart1Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Uart5Task */
osThreadId_t Uart5TaskHandle;
const osThreadAttr_t Uart5Task_attributes = {
  .name = "Uart5Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Uart4Task */
osThreadId_t Uart4TaskHandle;
const osThreadAttr_t Uart4Task_attributes = {
  .name = "Uart4Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void ControlTask(void *argument);
void PropellerTask(void *argument);
void Usart1Send(void *argument);
void Uart5Send(void *argument);
void Uart4Send(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Control */
  ControlHandle = osThreadNew(ControlTask, NULL, &Control_attributes);

  /* creation of Propeller */
  PropellerHandle = osThreadNew(PropellerTask, NULL, &Propeller_attributes);

  /* creation of Usart1Task */
  Usart1TaskHandle = osThreadNew(Usart1Send, NULL, &Usart1Task_attributes);

  /* creation of Uart5Task */
  Uart5TaskHandle = osThreadNew(Uart5Send, NULL, &Uart5Task_attributes);

  /* creation of Uart4Task */
  Uart4TaskHandle = osThreadNew(Uart4Send, NULL, &Uart4Task_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_ControlTask */
/**
* @brief Function implementing the Control_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_ControlTask */
void ControlTask(void *argument)
{
  /* USER CODE BEGIN ControlTask */
	
  /* Infinite loop */
	while(1)
	{
		uint32_t tick = osKernelGetTickCount();
		osDelayUntil(tick+100);									// delay 10 ticks periodically
		vControl_task();
		osThreadFlagsSet(PropellerHandle, 0x01);
	}
  /* USER CODE END ControlTask */
}

/* USER CODE BEGIN Header_PropellerTask */
/**
* @brief Function implementing the PropellerHandle thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_PropellerTask */
void PropellerTask(void *argument)
{
  /* USER CODE BEGIN PropellerTask */
  /* Infinite loop */
  while(1)
  {
    osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
		vPropeller_task();
  }
  /* USER CODE END PropellerTask */
}

/* USER CODE BEGIN Header_Usart1Send */
/**
* @brief Function implementing the Usart1Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Usart1Send */
void Usart1Send(void *argument)
{
  /* USER CODE BEGIN Usart1Send */
  /* Infinite loop */
  while(1)
	{
		uint32_t tick = osKernelGetTickCount();
		osDelayUntil(tick+100);									// delay 10 ticks periodically
		vUsart1SendData();
	}
  /* USER CODE END Usart1Send */
}

/* USER CODE BEGIN Header_Uart5Send */
/**
* @brief Function implementing the Uart5Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Uart5Send */
void Uart5Send(void *argument)
{
  /* USER CODE BEGIN Uart5Send */
  /* Infinite loop */
  while(1)
	{
		uint32_t tick = osKernelGetTickCount();
		osDelayUntil(tick+500);									// delay 10 ticks periodically
		vUsart5SendData();
	}
  /* USER CODE END Uart5Send */
}

/* USER CODE BEGIN Header_Uart4Send */
/**
* @brief Function implementing the Uart4Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Uart4Send */
void Uart4Send(void *argument)
{
  /* USER CODE BEGIN Uart4Send */
  /* Infinite loop */
  while(1)
	{
		uint32_t tick = osKernelGetTickCount();
		osDelayUntil(tick+200);									// delay 10 ticks periodically
		vUsart4SendData();
	}
  /* USER CODE END Uart4Send */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

